﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-4-18 11:27:31
 ******************************************************************************/

using System.Data;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.Utilities;
  

    /// <summary>
    /// PiUserOrganizeManager
    /// 用户账户组织关系表
    ///
    /// 修改纪录
    ///
    ///		2012-03-02 版本：1.0 EricHu 创建主键。
    ///
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>EricHu</name>
    ///		<date>2012-03-02</date>
    /// </author>
    /// </summary>
    public partial class PiUserOrganizeManager : DbCommonManager, IDbCommonManager
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public PiUserOrganizeManager()
        {
            if (base.dbProvider == null)
            {
                base.dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType, SystemInfo.RDIFrameworkDbConection);
            }
            base.CurrentTableName = PiUserOrganizeTable.TableName;
        }

        /// <summary>
        /// 构造函数
        /// <param name="tableName">指定表名</param>
        /// </summary>
        public PiUserOrganizeManager(string tableName)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        public PiUserOrganizeManager(IDbProvider dbProvider)
            : this()
        {
            DBProvider = dbProvider;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userInfo">用户信息</param>
        public PiUserOrganizeManager(UserInfo userInfo)
            : this()
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        public PiUserOrganizeManager(IDbProvider dbProvider, UserInfo userInfo)
            : this(dbProvider)
        {
            UserInfo = userInfo;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbProvider">数据库连接</param>
        /// <param name="userInfo">用户信息</param>
        /// <param name="tableName">指定表名</param>
        public PiUserOrganizeManager(IDbProvider dbProvider, UserInfo userInfo, string tableName)
            : this(dbProvider, userInfo)
        {
            base.CurrentTableName = tableName;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        /// <returns>主键</returns>
        public string Add(PiUserOrganizeEntity baseUserOrganizeEntity)
        {
            return this.AddEntity(baseUserOrganizeEntity);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        /// <param name="identity">自增量方式</param>
        /// <param name="returnId">返回主鍵</param>
        /// <returns>主键</returns>
        public string Add(PiUserOrganizeEntity baseUserOrganizeEntity, bool identity, bool returnId)
        {
            this.Identity = identity;
            this.ReturnId = returnId;
            return this.AddEntity(baseUserOrganizeEntity);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        public int Update(PiUserOrganizeEntity baseUserOrganizeEntity)
        {
            return this.UpdateEntity(baseUserOrganizeEntity);
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键</param>
        public PiUserOrganizeEntity GetEntity(int id)
        {
            PiUserOrganizeEntity baseUserOrganizeEntity = new PiUserOrganizeEntity(this.GetDT(PiUserOrganizeTable.FieldId, id));
            return baseUserOrganizeEntity;
        }

        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        public string AddEntity(PiUserOrganizeEntity baseUserOrganizeEntity)
        {
            string sequence = string.Empty;
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider, this.Identity, this.ReturnId);
            sqlBuilder.BeginInsert(this.CurrentTableName, PiUserOrganizeTable.FieldId);
            if (!this.Identity)
            {
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldId, baseUserOrganizeEntity.Id);
            }
            else
            {
                if (!this.ReturnId && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                {
                    if (DBProvider.CurrentDbType == CurrentDbType.Oracle)
                    {
                        sqlBuilder.SetFormula(PiUserOrganizeTable.FieldId, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL ");
                    }
                    if (DBProvider.CurrentDbType == CurrentDbType.DB2)
                    {
                        sqlBuilder.SetFormula(PiUserOrganizeTable.FieldId, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper());
                    }
                }
                else
                {
                    if (this.Identity && (DBProvider.CurrentDbType == CurrentDbType.Oracle || DBProvider.CurrentDbType == CurrentDbType.DB2))
                    {
                        if (baseUserOrganizeEntity.Id == null)
                        {
                            if (string.IsNullOrEmpty(sequence))
                            {
                                CiSequenceManager sequenceManager = new CiSequenceManager(DBProvider, this.Identity);
                                sequence = sequenceManager.GetSequence(this.CurrentTableName);
                            }
                            baseUserOrganizeEntity.Id = int.Parse(sequence);
                        }
                        sqlBuilder.SetValue(PiUserOrganizeTable.FieldId, baseUserOrganizeEntity.Id);
                    }
                }
            }
            this.SetEntity(sqlBuilder, baseUserOrganizeEntity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldCreateUserId, UserInfo.Id);
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldCreateBy, UserInfo.RealName);
            }
            sqlBuilder.SetDBNow(PiUserOrganizeTable.FieldCreateOn);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldModifyUserId, UserInfo.Id);
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldModifiedBy, UserInfo.RealName);
            }
            sqlBuilder.SetDBNow(PiUserOrganizeTable.FieldModifiedOn);
            if (DBProvider.CurrentDbType == CurrentDbType.SqlServer && this.Identity)
            {
                sequence = sqlBuilder.EndInsert().ToString();
            }
            else
            {
                sqlBuilder.EndInsert();
            }
            return sequence;
        }

        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        public int UpdateEntity(PiUserOrganizeEntity baseUserOrganizeEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(DBProvider);
            sqlBuilder.BeginUpdate(this.CurrentTableName);
            this.SetEntity(sqlBuilder, baseUserOrganizeEntity);
            if (UserInfo != null)
            {
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldModifyUserId, UserInfo.Id);
                sqlBuilder.SetValue(PiUserOrganizeTable.FieldModifiedBy, UserInfo.RealName);
            }
            sqlBuilder.SetDBNow(PiUserOrganizeTable.FieldModifiedOn);
            sqlBuilder.SetWhere(PiUserOrganizeTable.FieldId, baseUserOrganizeEntity.Id);
            return sqlBuilder.EndUpdate();
        }

        /// <summary>
        /// 设置实体
        /// </summary>
        /// <param name="baseUserOrganizeEntity">实体</param>
        private void SetEntity(SQLBuilder sqlBuilder, PiUserOrganizeEntity baseUserOrganizeEntity)
        {
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldUserId, baseUserOrganizeEntity.UserId);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldCompanyId, baseUserOrganizeEntity.CompanyId);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldDepartmentId, baseUserOrganizeEntity.DepartmentId);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldWorkgroupId, baseUserOrganizeEntity.WorkgroupId);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldRoleId, baseUserOrganizeEntity.RoleId);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldEnabled, baseUserOrganizeEntity.Enabled);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldDescription, baseUserOrganizeEntity.Description);
            sqlBuilder.SetValue(PiUserOrganizeTable.FieldDeleteMark, baseUserOrganizeEntity.DeleteMark);
        }

        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>影响行数</returns>
        public int Delete(int id)
        {
            return this.Delete(PiUserOrganizeTable.FieldId, id);
        }

        #region public string Add(BaseUserOrganizeEntity userOrganizeEntity, out string statusCode) 添加用户
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userOrganizeEntity">用户组织机构实体</param>
        /// <param name="statusCode">状态码</param>
        /// <returns>主键</returns>
        public string Add(PiUserOrganizeEntity userOrganizeEntity, out string statusCode)
        {
            string returnValue = string.Empty;
            // 判断数据是否重复了
            string[] names = new string[] { PiUserOrganizeTable.FieldDeleteMark, PiUserOrganizeTable.FieldEnabled, PiUserOrganizeTable.FieldCompanyId, PiUserOrganizeTable.FieldDepartmentId, PiUserOrganizeTable.FieldWorkgroupId, PiUserOrganizeTable.FieldRoleId };
            object[] values = new object[] { 0, 1, userOrganizeEntity.UserId, userOrganizeEntity.CompanyId, userOrganizeEntity.DepartmentId, userOrganizeEntity.WorkgroupId, userOrganizeEntity.RoleId };
            if (this.Exists(names, values))
            {
                // 用户名已重复
                statusCode = StatusCode.Exist.ToString();
            }
            else
            {
                returnValue = this.AddEntity(userOrganizeEntity);
                // 运行成功
                statusCode = StatusCode.OKAdd.ToString();
            }
            return returnValue;
        }
        #endregion

        /// <summary>
        /// 获得用户的组织机构兼职情况
        /// </summary>
        /// <param name="userId">用户主键</param>
        /// <returns>数据表</returns>
        public DataTable GetUserOrganizeDT(string userId)
        {
            string sqlQuery = " SELECT PiUserOrganize.* "
                            + "     , PiRole.Code AS RoleCode "
                            + "     , PiRole.RealName AS RoleName "
                            + "     , PiOrganize1.FullName AS CompanyName "
                            + "     , PiOrganize2.FullName AS DepartmentName "
                            + "     , PiOrganize3.FullName AS WorkgroupName "
                            + " FROM Base_UserOrganize LEFT OUTER JOIN "
                            + "     PiOrganize PiOrganize1 ON PiUserOrganize.CompanyId = PiOrganize1.Id LEFT OUTER JOIN "
                            + "     PiOrganize PiOrganize2 ON PiUserOrganize.DepartmentId = PiOrganize2.Id LEFT OUTER JOIN "
                            + "     PiOrganize PiOrganize3 ON PiUserOrganize.WorkgroupId = PiOrganize3.Id LEFT OUTER JOIN "
                            + "     PiRole ON PiUserOrganize.RoleId = PiRole.Id "
                            + " WHERE UserId = '" + userId + "'";
            return DBProvider.Fill(sqlQuery);
        }
    }
}
